/*******************************************************************************
 * Copyright 2017 Capital One Services, LLC and Bitwise, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 *******************************************************************************/
package hydrograph.engine.transformation.userfunctions.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
/**
 * The Class ListBasedReusableRow.
 *
 * @author Bitwise
 *
 */
public class ListBasedReusableRow extends ReusableRow {

	private ArrayList<Comparable> values;
	private HashMap<String, Integer> fieldPos;

	public ListBasedReusableRow(LinkedHashSet<String> fields) {
		super(fields);
		values = new ArrayList<Comparable>();
		fieldPos = new HashMap<String, Integer>();
		int i = -1;
		for (String field : fields) {
			i = i + 1;
			values.add(null);
			fieldPos.put(field, new Integer(i));
		}
	}

	@Override
	protected Comparable getFieldInternal(int index) {
		return (Comparable) values.get(index);
	}

	@Override
	protected Comparable getFieldInternal(String fieldName) {
		return (Comparable) values.get(fieldPos.get(fieldName));
	}

	@Override
	protected void setFieldInternal(int index, Comparable value) {
		values.set(index, value);

	}

	@Override
	protected void setFieldInternal(String fieldName, Comparable value) {
		values.set(fieldPos.get(fieldName), value);

	}

	@Override
	public Collection<Comparable> getFields() {
		return values;
	}

}
